<template>
  <div id="mySpacePage">
    <p>正在跳转，请稍后。。。</p>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { listSpaceVoByPageUsingPost } from '@/api/spaceController.ts'
import { message } from 'ant-design-vue'
import { onMounted } from 'vue'
import { SPACE_TYPE_ENUM } from '@/constants/space.ts'

const router = useRouter();
const loginUserStore = useLoginUserStore();

const checkUserSpace = async () => {
  // 用户未登录，跳转登录
  const loginUser = loginUserStore.loginUser;
  if (!loginUser) {
    router.replace('/user/login')
    return;
  }
  // 用户已登录，获取用户空间
  const res = await listSpaceVoByPageUsingPost({
    userId: loginUser.id,
    current: 1,
    pageSize: 1,
    spaceType: SPACE_TYPE_ENUM.PRIVATE,
  })
  if (res.data.code === 0) {
    // 用户有空间，跳转第一个空间
    if (res.data.data?.records?.length > 0) {
      const space = res.data.data.records[0];
      router.replace(`/space/${space.id}`)
    } else {
      router.replace('/add_space')
      message.warn("请创建个人空间")
    }
  } else {
    message.error("加载我的空间失败，"+res.data.message)
  }
}

onMounted(() => {
  checkUserSpace();
})
</script>
<style scoped>
#mySpacePage {
}
</style>
